const Router = require('koa-router');
const router = new Router();
const pool = require('../db');

// 获取所有歌曲
router.get('/api/songs', async (ctx) => {
    try {
        const [songs] = await pool.execute('SELECT id, title, artist, song_url, cover_url, created_at FROM songs ORDER BY id DESC');
        ctx.body = {
            success: true,
            data: songs
        };
    } catch (error) {
        console.error('获取歌曲列表失败:', error);
        ctx.status = 500;
        ctx.body = {
            success: false,
            message: '获取歌曲列表失败'
        };
    }
});

// 添加新歌曲
router.post('/api/songs', async (ctx) => {
    try {
        const { title, artist, song_url, cover_url } = ctx.request.body;
        
        if (!title || !artist) {
            ctx.status = 400;
            ctx.body = {
                success: false,
                message: '歌名和歌手不能为空'
            };
            return;
        }
        
        // 检查歌曲是否已存在
        const [existingSongs] = await pool.execute('SELECT id FROM songs WHERE title = ? AND artist = ?', [title, artist]);
        
        if (existingSongs.length > 0) {
            const [[row]] = await pool.execute('SELECT id, title, artist, song_url, cover_url, created_at FROM songs WHERE title = ? AND artist = ?', [title, artist]);
            ctx.body = {
                success: true,
                data: row,
                message: '歌曲已存在'
            };
            return;
        }
        
        // 提供默认值
        const defaultSongUrl = song_url || '/static/default-audio.mp3';
        const defaultCoverUrl = cover_url || 'https://ai-public.mastergo.com/ai/img_res/647cde825adf712e96b3bc66cb67656c.jpg';
        
        // 插入新歌曲
        const [result] = await pool.execute(
            'INSERT INTO songs (title, artist, song_url, cover_url) VALUES (?, ?, ?, ?)',
            [title, artist, defaultSongUrl, defaultCoverUrl]
        );

        const [[row]] = await pool.execute('SELECT id, title, artist, song_url, cover_url, created_at FROM songs WHERE id = ?', [result.insertId]);

        ctx.body = {
            success: true,
            data: row,
            message: '歌曲添加成功'
        };
        
        console.log('歌曲添加成功:', row);
        
    } catch (error) {
        console.error('添加歌曲失败:', error);
        ctx.status = 500;
        ctx.body = {
            success: false,
            message: '添加歌曲失败'
        };
    }
});

// 删除歌曲
router.delete('/api/songs/:id', async (ctx) => {
    try {
        const { id } = ctx.params;
        
        const [result] = await pool.execute('DELETE FROM songs WHERE id = ?', [id]);
        
        if (result.affectedRows === 0) {
            ctx.status = 404;
            ctx.body = {
                success: false,
                message: '歌曲不存在'
            };
            return;
        }
        
        ctx.body = {
            success: true,
            message: '歌曲删除成功'
        };
        
    } catch (error) {
        console.error('删除歌曲失败:', error);
        ctx.status = 500;
        ctx.body = {
            success: false,
            message: '删除歌曲失败'
        };
    }
});

module.exports = router;

